# 买卖股票的最佳时机 II


class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        length = len(prices)
        result = 0
        i = 0
        while i < length:
            # 选择买入的位置
            if i+1 < length and prices[i] < prices[i+1]:
                buy = prices[i]
                sell = prices[i+1]
                j = i + 2
                while j < length:
                    if sell > prices[j]:
                        break
                    else:
                        sell = prices[j]
                        j = j + 1
                i = j
                result += sell - buy
            else:
                i = i + 1

        return result
